// 页面切换功能
function switchPage(pageId) {
    // 隐藏所有页面
    document.querySelectorAll('.page').forEach(page => {
        page.classList.remove('active');
    });

    // 显示目标页面
    document.getElementById(pageId).classList.add('active');

    // 更新导航标题
    const titles = {
        'activitySquare': '活动广场',
        'createTeam': '创建队伍',
        'myTeam': '我的队伍',
        'uploadPhoto': '上传照片'
    };
    document.getElementById('navbarTitle').textContent = titles[pageId];

    // 更新底部导航选中状态
    document.querySelectorAll('.tabbar-item').forEach(item => {
        item.classList.remove('active');
    });
    document.querySelectorAll('.tabbar-item').forEach(item => {
        if (item.getAttribute('onclick').includes(pageId)) {
            item.classList.add('active');
        }
    });
}

// 检查是否可以提交队伍创建表单
function checkSubmit() {
    const school = document.getElementById('schoolInput').value.trim();
    const teamName = document.getElementById('teamNameInput').value.trim();
    const submitBtn = document.getElementById('submitBtn');

    submitBtn.disabled = !(school && teamName);
}

// 提交队伍创建
function submitTeam() {
    const school = document.getElementById('schoolInput').value.trim();
    const teamName = document.getElementById('teamNameInput').value.trim();

    showToast(`队伍创建申请已提交！\n中学：${school}\n队伍名称：${teamName}\n请等待招办审批`);

    // 模拟提交后清空表单
    document.getElementById('schoolInput').value = '';
    document.getElementById('teamNameInput').value = '';
    checkSubmit();
}

// 显示队伍详情（跳转到我的队伍页面）
function showTeamDetail() {
    switchPage('myTeam');
}

// 照片上传相关功能
let selectedPhotos = [];
const maxPhotos = 5;

function selectPhotos() {
    const remaining = maxPhotos - selectedPhotos.length;
    if (remaining <= 0) {
        showToast('已达到最大上传数量');
        return;
    }

    // 模拟选择照片
    const count = Math.min(remaining, 3); // 一次最多选3张
    for (let i = 0; i < count; i++) {
        // 使用随机图片作为示例
        const randomId = Math.floor(Math.random() * 1000);
        selectedPhotos.push(`https://picsum.photos/200/200?random=${randomId}`);
    }

    updatePhotoList();
}

function updatePhotoList() {
    const photoList = document.getElementById('photoList');
    const remainTip = document.getElementById('remainTip');
    const submitBtn = document.getElementById('submitPhotoBtn');

    // 清空列表
    photoList.innerHTML = '';

    // 添加选中的照片
    selectedPhotos.forEach((photo, index) => {
        const photoItem = document.createElement('div');
        photoItem.className = 'photo-item';
        photoItem.innerHTML = `
            <img src="${photo}" class="photo" alt="活动照片">
            <button class="delete-btn" onclick="deletePhoto(${index})">×</button>
        `;
        photoList.appendChild(photoItem);
    });

    // 更新剩余数量
    const remaining = maxPhotos - selectedPhotos.length;
    remainTip.textContent = `可继续上传${remaining}张照片审核`;

    // 更新提交按钮状态
    submitBtn.disabled = selectedPhotos.length === 0;
}

function deletePhoto(index) {
    selectedPhotos.splice(index, 1);
    updatePhotoList();
}

function submitPhotos() {
    if (selectedPhotos.length === 0) {
        showToast('请先选择照片');
        return;
    }

    showToast(`已提交${selectedPhotos.length}张照片审核，审核通过后可见`);

    // 模拟提交后返回我的队伍页面
    setTimeout(() => {
        switchPage('myTeam');

        // 更新照片状态为审核中
        document.getElementById('photoStatus').innerHTML = '审核中';
        document.getElementById('photoPlaceholder').style.display = 'block';
    }, 1500);
}

// 提示弹窗功能
function showToast(message) {
    document.getElementById('toastMessage').textContent = message;
    document.getElementById('toastModal').classList.add('active');
}

function hideToast() {
    document.getElementById('toastModal').classList.remove('active');
}

// 初始化
document.addEventListener('DOMContentLoaded', function() {
    // 初始化照片列表
    updatePhotoList();
});